package Thread.interrupt_thread;

/**
 * 使用Thread.interrupted()静态方法
 */
//public class property {
//    private static class MyRunnable implements Runnable {
//
//        @Override
//        public void run() {
//            //静态方法，判断当前线程是否被中断
//            while (!Thread.interrupted()) {
//                System.out.println(Thread.currentThread().getName() + "别烦我，在打游戏呢~~~");
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    //当线程中断时，会抛出中断异常
//                    System.out.println("有个老六在偷家~~~");
//                    break;
//                }
//            }
//            System.out.println(Thread.currentThread().getName()+"额，水晶被偷了~~~");
//        }
//    }
//
//    public static void main(String[] args) throws InterruptedException {
//        MyRunnable m1=new MyRunnable();
//        Thread thread=new Thread(m1,"线程一~~");
//        System.out.println("正在快乐的吃兵线中~~~");
//        thread.start();
//        //让主线程暂停5秒，中断子线程
//        Thread.sleep(5000);
//        thread.interrupt();
//    }
//}


/**
 * Thread对象的成员方法isInterrupted()
 */
public class property {
    private static class MyRunnable implements Runnable {

        @Override
        public void run() {
            //成员方法，判断当前线程是否中断
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName() + "别烦我，在打游戏呢~~~");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //当线程中断时，会抛出中断异常
                    System.out.println("有个老六在偷家~~~");
                    break;
                }
            }
            System.out.println(Thread.currentThread().getName()+"额，水晶被偷了~~~");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable m1=new MyRunnable();
        Thread thread=new Thread(m1,"线程一~~");
        System.out.println("正在快乐的吃兵线中~~~");
        thread.start();
        //让主线程暂停5秒，中断子线程
        Thread.sleep(5000);
        thread.interrupt();
    }
}
